<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            border: 1px solid #000;
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="box">

    </div>

    <script>
        const box = document.getElementById('box')

        let x = 0
        box.onmousedown = () => {

            const evt = window.event;
            console.log("鼠标起始的值：", evt.x);
            let startX = evt.x
            window.onmousemove = (e) => {
                let offsetX = e.x - evt.x
                x += offsetX
                box.style.left = `${offsetX}px`
                console.log("移动的差值:", offsetX);
                startX = e.x

            }
            window.onmouseup = () => {
                window.onmousemove = null
            }
        }
    </script>
</body>

</html>